import Ember from 'ember';
import BaseBusiness from '../base-business';
import Pagination from '../pagination';

export default BaseBusiness.extend(Pagination,{
  header_title:'房间列表',

  model:function(){
    return {};
  },
  buildQueryParams:function(){
    var params=this.pagiParamsSet();
    var curController = this.getCurrentController();
    var filter={};
    var sort;
    if (curController) {
        if (curController.get('queryCondition')) {
          filter = $.extend({}, filter, {'name@$like':curController.get('queryCondition')});
        }
    }
    if(this.get('orientationID')){
      filter = $.extend({}, filter, {'[orientation][id]':this.get('orientationID')});
    }
    if(this.get('roomTypeID')){
      filter = $.extend({}, filter, {'[roomType][id]':this.get('roomTypeID')});

    }
    params.filter = filter;
    sort = {
        '[floor][building][name]':'asc',
        '[floor][name]':'asc',
         'name':'asc'
    };
    params.sort = sort;
    return params;
  },
  doQuery:function(){
    var params=this.buildQueryParams();
    var roomList=this.findPaged('room',params,function(roomList){});
    this.getCurrentController().set("roomList", roomList);
    this.getCurrentController().set("roomTypeID",'');
  },
  actions:{
    search:function(){
      this.doQuery();
    },
    roomSelect(roomType){
      if(roomType){
        this.set('roomTypeID',roomType.id);
      }else{
        this.set('roomTypeID','');
      }
      this.doQuery();
    },
    orientationSelect(orientation){
      if(orientation){
        this.set('orientationID',orientation.id);
      }else{
        this.set('orientationID','');
      }

      this.doQuery();
    }
  },
  setupController: function(controller,model){

    this.set('roomTypeID','');
    this.set('orientationID','');
    this.doQuery();
    var queryCondition = controller.get('input');
    controller.set('queryCondition', queryCondition);
    this._super(controller,model);
  }
});
